/***********************************************************************************/
/*                                                                                 */
/*                                                                                 */
/*    @Project:Acme-ClusterMonitor                                                 */
/*    @ File Name : mainwindow.cpp                                                 */
/*    @ Date : 2011-8-5                                                            */
/*    @ Author : Li Chengyu                                                        */
/*    @Description :                                                               */
/*                                                                                 */
/*                                                                                 */
/***********************************************************************************/

#include "mainwindow.h"

MainWindow::MainWindow()
    : QMainWindow()
{
    server = new JServer(this);
    server->listen(QHostAddress::Any,1234);
    QFont font("ZYSong18030",12);
    setFont(font);
    setWindowTitle(tr("ClusterMonitor"));
    this->setWindowIcon(QIcon(":/image/icon.ico"));
    widget = new QWidget(this);
    this->setCentralWidget(widget);
    winLayout = new QGridLayout(widget);
    destop = QApplication::desktop();
    resize(destop->width() , destop->height());
    hour = 0;
    minute = 0;
    second = 0;

    sideLabel = new QLabel(widget);
    Cpu = new QLabel(widget);
    Memory = new QLabel(widget);
    Flow = new QLabel(widget);
    Temperature = new QLabel(widget);
    winLayout->addWidget(sideLabel , 0 , 0);
    winLayout->addWidget(Cpu , 0 , 1 , Qt::AlignTop);
    winLayout->addWidget(Memory , 0 , 2 , Qt::AlignTop);
    winLayout->addWidget(Flow , 1 , 1 , Qt::AlignTop);
    winLayout->addWidget(Temperature , 1 , 2 , Qt::AlignTop);
    widget->setLayout(winLayout);
    sideLabel->setFixedWidth(200);
    sideLabel->resize(200 , destop->height()/2);
    Cpu->setPixmap(QPixmap(":/image/cpu.png"));
    Cpu->resize(QPixmap(":/image/cpu.png").width(),QPixmap(":/image/cpu.png").height());
    Memory->setPixmap(QPixmap(":/image/mem.png"));
    Memory->resize(QPixmap(":/image/mem.png").width(),QPixmap(":/image/mem.png").height());
    Flow->setPixmap(QPixmap(":/image/flow.png"));
    Flow->resize(QPixmap(":/image/flow.png").width(),QPixmap(":/image/flow.png").height());
    Temperature->setPixmap(QPixmap(":/image/tem.png"));
    Temperature->resize(QPixmap(":/image/tem.png").width(),QPixmap(":/image/tem.png").height());

    pushText = new QLabel(sideLabel);
    pushText->resize(180 , 50);
    pushText->move(5 , 20);
    pushText->setWordWrap(true);
    pushText->setFont(QFont("Timer" , 12));
    pushText->setText("Click to view the detailed information");

    cpuButton = new QPushButton("CPU" , sideLabel);
    cpuButton->resize(100 , 40);
    cpuButton->move(50 , 80);
    memButton = new QPushButton("MEMORY" , sideLabel);
    memButton->resize(100 , 40);
    memButton->move(50 , 130);
    flowButton = new QPushButton("TRAFFIC" , sideLabel);
    flowButton->resize(100 , 40);
    flowButton->move(50 , 180);
    exit = new QPushButton("QUIT" , sideLabel);
    exit->resize(100 , 40);
    exit->move(50 , 230);

    cpuCurve = new DrawCurve(Cpu);
    cpuCurve->move(104 , 17);
    cpuCurve->cpuAllDraw = true;
    memoryCurve = new DrawCurve(Memory);
    memoryCurve->move(99 , 17);
    memoryCurve->memAllDraw = true;
    flowCurve = new DrawCurve(Flow);
    flowCurve->move(104 , 20);
    flowCurve->flowAllDraw = true;
    temperatureCurve = new DrawCurve(Temperature);
    temperatureCurve->move(99 , 23);
    temperatureCurve->temAllDraw = true;
    nodeTextLabel = new QLabel(this);
    onlineNodeLabel = new QLabel(this);
    timeInfoLabel = new QLabel(this);
    nodeTextLabel->resize(195 , 50);
    onlineNodeLabel->resize(195 , 50);
    timeInfoLabel->resize(195 , 50);
    nodeTextLabel->move(5 , height()/2);
    onlineNodeLabel->move(5 , (height()/2 + 50));
    timeInfoLabel->move(5 , (height()/2 + 100));
    nodeTextLabel->setWordWrap(true);
    onlineNodeLabel->setWordWrap(true);
    timeInfoLabel->setWordWrap(true);
    nodeTextLabel->setFont(QFont("ZYSong18030" , 14));
    onlineNodeLabel->setFont(QFont("ZYSong18030" , 14));
    timeInfoLabel->setFont(QFont("ZYSong18030" , 14));
    nodeTextLabel->setText("Total Nodes: 0");
    onlineNodeLabel->setText("Online Nodes: 0");
    timeInfoLabel->setText("Uptime: 0:0:0");
    //this->dataRead();
    this->connect(cpuButton , SIGNAL(clicked()) , this , SLOT(cpuDockWindow()));
    this->connect(memButton , SIGNAL(clicked()) , this , SLOT(memDockWindow()));
    this->connect(flowButton , SIGNAL(clicked()) , this , SLOT(flowDockWindow()));
    this->connect(exit , SIGNAL(clicked()) , this , SLOT(close()));
    readDataTimeId = startTimer(2000);
    showDataTimeId = startTimer(2000);
    runTimeId = startTimer(1000);
}

MainWindow::~MainWindow()
{
    delete cpuCurve;
    delete memoryCurve;
    delete flowCurve;
    delete temperatureCurve;
    delete pushText;
    delete cpuButton;
    delete memButton;
    delete flowButton;
    delete nodeTextLabel;
    delete onlineNodeLabel;
    delete timeInfoLabel;
    delete exit;
    delete Cpu;
    delete Memory;
    delete Temperature;
    delete Flow;
    delete sideLabel;
    delete winLayout;
    delete widget;
    delete server;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter Painter(this);
    Painter.drawLine(200 , 0 , 200 , this->height());
}

void MainWindow::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == readDataTimeId)
        this->dataRead();
    if(e->timerId() == showDataTimeId)
    {
        nodeTextLabel->setText("Total Nodes: " + QString::number(server->clientList.size()));
        onlineNodeLabel->setText("Online Nodes: " + QString::number(server->data.onlineHostNum));
    }
    if(e->timerId() == runTimeId)
    {
        second += 1;
        if(second == 60)
        {
            minute += 1;
            second = 0;
        }
        if(minute == 60)
        {
            hour += 1;
            minute = 0;
        }
        timeInfoLabel->setText("Uptime: " + QString::number(hour) + ":" + QString::number(minute) + ":" + QString::number(second));
    }
}

void MainWindow::cpuDockWindow()
{
    cpuDockWin = new DockWindow(this);
    cpuDockWin->cpuDock();
    cpuDockWin->show();
}

void MainWindow::memDockWindow()
{
    memDockWin = new DockWindow(this);
    memDockWin->memDock();
    memDockWin->show();
}

void MainWindow::flowDockWindow()
{
    flowDockWin = new DockWindow(this);
    flowDockWin->flowDock();
    flowDockWin->show();

}

void MainWindow::dataRead()
{
    if(server->clientList.size() > 0)
    {
        for(int i=36 ; i>=0; i--)
        {
            cpuInfo.user.average[i+1] = cpuInfo.user.average[i];
            cpuInfo.user.max[i+1] = cpuInfo.user.max[i];
            cpuInfo.user.min[i+1] = cpuInfo.user.min[i];
            cpuInfo.nice.average[i+1] = cpuInfo.nice.average[i];
            cpuInfo.nice.max[i+1] = cpuInfo.nice.max[i];
            cpuInfo.nice.min[i+1] = cpuInfo.nice.min[i];
            cpuInfo.system.average[i+1] = cpuInfo.system.average[i];
            cpuInfo.system.max[i+1] = cpuInfo.system.max[i];
            cpuInfo.system.min[i+1] = cpuInfo.system.min[i];
            cpuInfo.wait.average[i+1] = cpuInfo.wait.average[i];
            cpuInfo.wait.max[i+1] = cpuInfo.wait.max[i];
            cpuInfo.wait.min[i+1] = cpuInfo.wait.min[i];
            cpuInfo.idle.average[i+1] = cpuInfo.idle.average[i];
            cpuInfo.idle.max[i+1] = cpuInfo.idle.max[i];
            cpuInfo.idle.min[i+1] = cpuInfo.idle.min[i];
            memoryInfo.used.average[i+1] = memoryInfo.used.average[i];
            memoryInfo.used.max[i+1] = memoryInfo.used.max[i];
            memoryInfo.used.min[i+1] = memoryInfo.used.min[i];
            memoryInfo.shared.average[i+1] = memoryInfo.shared.average[i];
            memoryInfo.shared.max[i+1] = memoryInfo.shared.max[i];
            memoryInfo.shared.min[i+1] = memoryInfo.shared.min[i];
            memoryInfo.cached.average[i+1] = memoryInfo.cached.average[i];
            memoryInfo.cached.max[i+1] = memoryInfo.cached.max[i];
            memoryInfo.cached.min[i+1] = memoryInfo.cached.min[i];
            memoryInfo.buffered.average[i+1] = memoryInfo.buffered.average[i];
            memoryInfo.buffered.max[i+1] = memoryInfo.buffered.max[i];
            memoryInfo.buffered.min[i+1] = memoryInfo.buffered.min[i];
            memoryInfo.swapped.average[i+1] = memoryInfo.swapped.average[i];
            memoryInfo.swapped.max[i+1] = memoryInfo.swapped.max[i];
            memoryInfo.swapped.min[i+1] = memoryInfo.swapped.min[i];
            flowInfo.in.average[i+1] = flowInfo.in.average[i];
            flowInfo.in.max[i+1] = flowInfo.in.max[i];
            flowInfo.in.min[i+1] = flowInfo.in.min[i];
            flowInfo.out.average[i+1] = flowInfo.out.average[i];
            flowInfo.out.max[i+1] = flowInfo.out.max[i];
            flowInfo.out.min[i+1] = flowInfo.out.min[i];
            temperatureInfo.temperature.average[i+1] = temperatureInfo.temperature.average[i];
            temperatureInfo.temperature.max[i+1] = temperatureInfo.temperature.max[i];
            temperatureInfo.temperature.min[i+1] = temperatureInfo.temperature.min[i];
        }
        cpuInfo.user.average[0] = server->data.cpu.user.average/10;
        cpuInfo.user.max[0] = server->data.cpu.user.max.data/10;
        cpuInfo.user.max_id = server->data.cpu.user.max.node_id;
        cpuInfo.user.max_node_name = server->clientList[cpuInfo.user.max_id]->info.hostname;
        cpuInfo.user.min[0] = server->data.cpu.user.min.data/10;
        cpuInfo.user.min_id = server->data.cpu.user.min.node_id;
        cpuInfo.user.min_node_name = server->clientList[cpuInfo.user.min_id]->info.hostname;
        cpuInfo.nice.average[0] = server->data.cpu.nice.average/10;
        cpuInfo.nice.max[0] = server->data.cpu.nice.max.data/10;
        cpuInfo.nice.max_id = server->data.cpu.nice.max.node_id;
        cpuInfo.nice.max_node_name = server->clientList[cpuInfo.nice.max_id]->info.hostname;
        cpuInfo.nice.min[0] = server->data.cpu.nice.min.data/10;
        cpuInfo.nice.min_id = server->data.cpu.nice.min.node_id;
        cpuInfo.nice.min_node_name = server->clientList[cpuInfo.nice.min_id]->info.hostname;
        cpuInfo.system.average[0] = server->data.cpu.system.average/10;
        cpuInfo.system.max[0] = server->data.cpu.system.max.data/10;
        cpuInfo.system.max_id = server->data.cpu.system.max.node_id;
        cpuInfo.system.max_node_name = server->clientList[cpuInfo.system.max_id]->info.hostname;
        cpuInfo.system.min[0] = server->data.cpu.system.min.data/10;
        cpuInfo.system.min_id = server->data.cpu.system.min.node_id;
        cpuInfo.system.min_node_name = server->clientList[cpuInfo.system.min_id]->info.hostname;
        cpuInfo.wait.average[0] = server->data.cpu.wait.average/10;
        cpuInfo.wait.max[0] = server->data.cpu.wait.max.data/10;
        cpuInfo.wait.max_id = server->data.cpu.wait.max.node_id;
        cpuInfo.wait.max_node_name = server->clientList[cpuInfo.wait.max_id]->info.hostname;
        cpuInfo.wait.min[0] = server->data.cpu.wait.min.data/10;
        cpuInfo.wait.min_id = server->data.cpu.wait.min.node_id;
        cpuInfo.wait.min_node_name = server->clientList[cpuInfo.wait.min_id]->info.hostname;
        cpuInfo.idle.average[0] = server->data.cpu.idle.average/10;
        cpuInfo.idle.max[0] = server->data.cpu.idle.max.data/10;
        cpuInfo.idle.max_id = server->data.cpu.idle.max.node_id;
        cpuInfo.idle.max_node_name = server->clientList[cpuInfo.idle.max_id]->info.hostname;
        cpuInfo.idle.min[0] = server->data.cpu.idle.min.data/10;
        cpuInfo.idle.min_id = server->data.cpu.idle.min.node_id;
        cpuInfo.idle.min_node_name = server->clientList[cpuInfo.idle.min_id]->info.hostname;
        memoryInfo.used.average[0] = server->data.mem.used.average/10;
        memoryInfo.used.max[0] = server->data.mem.used.max.data/10;
        memoryInfo.used.max_id = server->data.mem.used.max.node_id;
        memoryInfo.used.max_node_name = server->clientList[memoryInfo.used.max_id]->info.hostname;
        memoryInfo.used.min[0] = server->data.mem.used.min.data/10;
        memoryInfo.used.min_id = server->data.mem.used.min.node_id;
        memoryInfo.used.min_node_name = server->clientList[memoryInfo.used.min_id]->info.hostname;
        memoryInfo.shared.average[0] = server->data.mem.shared.average/10;
        memoryInfo.shared.max[0] = server->data.mem.shared.max.data/10;
        memoryInfo.shared.max_id = server->data.mem.shared.max.node_id;
        memoryInfo.shared.max_node_name = server->clientList[memoryInfo.shared.max_id]->info.hostname;
        memoryInfo.shared.min[0] = server->data.mem.shared.min.data/10;
        memoryInfo.shared.min_id = server->data.mem.shared.min.node_id;
        memoryInfo.shared.min_node_name = server->clientList[memoryInfo.shared.min_id]->info.hostname;
        memoryInfo.cached.average[0] = server->data.mem.cached.average/10;
        memoryInfo.cached.max[0] = server->data.mem.cached.max.data/10;
        memoryInfo.cached.max_id = server->data.mem.cached.max.node_id;
        memoryInfo.cached.max_node_name = server->clientList[memoryInfo.cached.max_id]->info.hostname;
        memoryInfo.cached.min[0] = server->data.mem.cached.min.data/10;
        memoryInfo.cached.min_id = server->data.mem.cached.min.node_id;
        memoryInfo.cached.min_node_name = server->clientList[memoryInfo.cached.min_id]->info.hostname;
        memoryInfo.buffered.average[0] = server->data.mem.buffered.average/10;
        memoryInfo.buffered.max[0] = server->data.mem.buffered.max.data/10;
        memoryInfo.buffered.max_id = server->data.mem.buffered.max.node_id;
        memoryInfo.buffered.max_node_name = server->clientList[memoryInfo.buffered.max_id]->info.hostname;
        memoryInfo.buffered.min[0] = server->data.mem.buffered.min.data/10;
        memoryInfo.buffered.min_id = server->data.mem.buffered.min.node_id;
        memoryInfo.buffered.min_node_name = server->clientList[memoryInfo.buffered.min_id]->info.hostname;
        memoryInfo.swapped.average[0] = server->data.mem.swapped.average/10;
        memoryInfo.swapped.max[0] = server->data.mem.swapped.max.data/10;
        memoryInfo.swapped.max_id = server->data.mem.swapped.max.node_id;
        memoryInfo.swapped.max_node_name = server->clientList[memoryInfo.swapped.max_id]->info.hostname;
        memoryInfo.swapped.min[0] = server->data.mem.swapped.min.data/10;
        memoryInfo.swapped.min_id = server->data.mem.swapped.min.node_id;
        memoryInfo.swapped.min_node_name = server->clientList[memoryInfo.swapped.min_id]->info.hostname;
        flowInfo.in.average[0] = server->data.traffic.in.average/10000;
        flowInfo.in.max[0] = server->data.traffic.in.max.data/10000;
        flowInfo.in.max_id = server->data.traffic.in.max.node_id;
        flowInfo.in.max_node_name = server->clientList[flowInfo.in.max_id]->info.hostname;
        flowInfo.in.min[0] = server->data.traffic.in.min.data/10000;
        flowInfo.in.min_id = server->data.traffic.in.min.node_id;
        flowInfo.in.min_node_name = server->clientList[flowInfo.in.min_id]->info.hostname;
        flowInfo.out.average[0] = server->data.traffic.out.average/10000;
        flowInfo.out.max[0] = server->data.traffic.out.max.data/10000;
        flowInfo.out.max_id = server->data.traffic.out.max.node_id;
        flowInfo.out.max_node_name = server->clientList[flowInfo.out.max_id]->info.hostname;
        flowInfo.out.min[0] = server->data.traffic.out.min.data/10000;
        flowInfo.out.min_id = server->data.traffic.out.min.node_id;
        flowInfo.out.min_node_name = server->clientList[flowInfo.out.min_id]->info.hostname;
        temperatureInfo.temperature.average[0] = server->data.temperature.temp.average;
        temperatureInfo.temperature.max[0] = server->data.temperature.temp.max.data;
        temperatureInfo.temperature.max_id = server->data.temperature.temp.max.node_id;
        temperatureInfo.temperature.min[0] = server->data.temperature.temp.min.data;
        temperatureInfo.temperature.min_id = server->data.temperature.temp.min.node_id;
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    switch(QMessageBox::question(this , tr("Quit") , tr("Are You Sure To Quit ?") ,
                                    tr("YES") , tr("NO") , 0 , 1))
    {
    case 0:
        event->accept();
        break;
    case 1:
    default:
        event->ignore();
        break;
    }
}
